package com.atguigu.PracticalProject.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class IkUtil {

    public static List<String> split(String keyword){
        ArrayList<String> result = new ArrayList<>();

        StringReader reader = new StringReader(keyword);
        IKSegmenter seg = new IKSegmenter(reader, true);

        Lexeme le = null;

        try {
            le = seg.next();
            while (le != null) {
                String kw = le.getLexemeText();
                result.add(kw);
                le = seg.next();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  result;
    }

    public static List<String> split(String keyword, boolean distinct) {
        if (distinct) {
            List<String> list = split(keyword);
            HashSet<String> set = new HashSet<>(list);
            return new ArrayList<>(set);
        }else {
            return split(keyword);
        }
    }
}
